package org.jks.controller;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jks.config.Config;
import org.jks.response.model.ActionResult;
import org.jks.response.model.UIFile;
import org.jks.util.DBUtil;
import org.jks.util.FileUtil;
import org.jks.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/db_sec")
public class DataSecurityController {

	@RequestMapping("/setting")
	public ModelAndView setting(HttpServletRequest request,
			HttpSession session, HttpServletResponse response,
			Map<String, Object> model) {
		Config.init(request, response, session, model);
		return new ModelAndView("sysMgr/db_sec_setting", model);
	}

	@RequestMapping("/backup")
	public void backup(HttpServletRequest request, HttpServletResponse response) {
		ActionResult ar = new ActionResult();
		ResourceBundle rb = ResourceBundle.getBundle("config");
		String binPath = rb.getString("backup.binpath");
		String username = rb.getString("jdbc.username");
		String password = rb.getString("jdbc.password");
		String dbname = rb.getString("backup.dbname");
		String bakPath = rb.getString("backup.bakpath");
		if(bakPath.startsWith("/")){
			bakPath = request.getServletContext().getRealPath("/") + bakPath;
		}
		File file = new File(bakPath);
		if (!file.exists()) {
			file.mkdirs();
		}
		if (DBUtil.backup(binPath, username, password, dbname, bakPath)) {
			ar.setSuccess(true);
			ar.setMsg("备份完成");
		} else {
			ar.setMsg("备份失败");
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/reload")
	public void reload(@RequestParam String name, HttpServletRequest request, HttpServletResponse response) {
		ActionResult ar = new ActionResult();
		ResourceBundle rb = ResourceBundle.getBundle("config");
		String binPath = rb.getString("backup.binpath");
		String username = rb.getString("jdbc.username");
		String password = rb.getString("jdbc.password");
		String dbname = rb.getString("backup.dbname");
		String bakPath = rb.getString("backup.bakpath");
		if(bakPath.startsWith("/") || bakPath.startsWith("\\")){
			bakPath = request.getServletContext().getRealPath("/") + bakPath;
		}
		bakPath += File.separator + name;
		File file = new File(bakPath);
		if (!file.exists()) {
			file.mkdirs();
		}
		if (DBUtil.restore(binPath, username, password, dbname, bakPath)) {
			ar.setSuccess(true);
			ar.setMsg("恢复完成");
		} else {
			ar.setMsg("恢复失败");
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/files")
	public void files(HttpServletRequest request, HttpServletResponse response){
		ResourceBundle rb = ResourceBundle.getBundle("config");
		String basePath = request.getServletContext().getRealPath("/");
		String bakPath = rb.getString("backup.bakpath");
		if(bakPath.startsWith("/")){
			bakPath = basePath + bakPath;
		}
		ActionResult ar = new ActionResult();
		List<UIFile> uiFiles = FileUtil.list(basePath, bakPath);
		if(uiFiles != null){
			ar.setSuccess(true);
			ar.setData(uiFiles);
		}
		else{
			ar.setMsg("列举文件失败");
		}
		ResponseUtil.returnJson(response, ar);
	}

	@RequestMapping("/restore")
	public ModelAndView restore(HttpServletRequest request,
			HttpSession session, HttpServletResponse response,
			Map<String, Object> model) {
		Config.init(request, response, session, model);
		return new ModelAndView("sysMgr/db_sec_restore", model);
	}
}
